package com.example.klaraservice.controller;

import cn.hutool.core.util.StrUtil;
import com.example.klaraservice.dao.AddressMapper;
import com.example.klaraservice.pojo.Manger;
import com.example.klaraservice.pojo.User;
import com.example.klaraservice.service.MangerService;
import com.example.klaraservice.service.UserService;
import com.example.klaraservice.vo.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author : Glacialerisa
 * @date : 2023/6/1 16:22
 * @desc :
 */
@Api(tags = "用户")
@RestController
@RequestMapping("/api/user")
public class UserController {
    @Resource
    private UserService userService;
    @Resource
    private MangerService mangerService;
    @ApiOperation("用户登录")
    @GetMapping("/login")
    public R login(@RequestParam String phone,@RequestParam String password){
        if (phone!=null&&password!=null){
            User userLogin = userService.login(phone, password);
            Manger mangerLogin = mangerService.login(phone, password);
            if (mangerLogin!=null){
                return R.ok().data("manger",mangerLogin);
            }else if (userLogin!=null){
                return R.ok().data("user",userLogin);
            }else {
                return R.error();
            }
        }else {
            return R.error();
        }
    }
    @ApiOperation("获取一个用户")
    @GetMapping("/getUserByPhone")
    public R getUserByPhone(@RequestParam String phone){
        User user = userService.selectByPhone(phone);
        if(user!=null){
            return R.ok().data("user",user);
        }
        return R.error();
    }
    @ApiOperation("模糊查询用户")
    @GetMapping("/getUsers")
    public R getUser(@RequestParam(defaultValue = "1") int pageNum,@RequestParam(defaultValue = "10")int pageSize,@RequestParam(defaultValue = "") String name){
        Map<String, Object> data = userService.selectAll(pageNum, pageSize, name);
        if (data!=null){
            return R.ok().data("user", data);
        }
        return R.error();
    }
    @ApiOperation("注册用户")
    @PostMapping("/register")
    public R register(@RequestParam String phone,@RequestParam String password){
//        System.out.println(phone+"-----------"+password);
        //TODO password加密
        boolean result = userService.register(phone, password);
        if (result){
            return R.ok();
        }else {
            return R.error();
        }
    }
    @ApiOperation("修改用户信息")
    @PutMapping("/updateUserInfo")
    public R updateUser(@RequestBody User user){
        boolean result = userService.updateUserInfo(user);
        if (result){
            return R.ok();
        }else {
            return R.error();
        }
    }
}
